package com.pur.zsms.shopmanage.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.sf.cglib.beans.BeanCopier;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.CipherUtil;
import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.dao.SupplierShopDao;
import com.pur.zsms.foodmanage.food.po.FoodPo;
import com.pur.zsms.foodmanage.food.vo.FoodVo;
import com.pur.zsms.matermanage.mater.dao.IMaterDao;
import com.pur.zsms.matermanage.mater.po.MaterLevelPo;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.pmsnmanage.menu.po.MenuPo;
import com.pur.zsms.pmsnmanage.role.dao.IRoleDao;
import com.pur.zsms.pmsnmanage.role.po.RolePo;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.dao.IUserDao;
import com.pur.zsms.pmsnmanage.user.po.LdAddressPo;
import com.pur.zsms.pmsnmanage.user.po.UserLevelPo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.po.UserRolePo;
import com.pur.zsms.pmsnmanage.user.service.IUserService;
import com.pur.zsms.pmsnmanage.user.vo.LdAddressVo;
import com.pur.zsms.pmsnmanage.user.vo.UserVo;
import com.pur.zsms.po.SupplierShopPo;
import com.pur.zsms.po.SupplierShopUserPo;
import com.pur.zsms.shopmanage.dao.IShopDao;
import com.pur.zsms.shopmanage.po.ShopPo;
import com.pur.zsms.shopmanage.po.ShopRaisePricePo;
import com.pur.zsms.shopmanage.po.ShopUserPo;
import com.pur.zsms.shopmanage.service.IShopService;
import com.pur.zsms.shopmanage.vo.ShopRaisePriceVo;
import com.pur.zsms.shopmanage.vo.ShopVo;
import com.pur.zsms.supplier.dao.SupplierDao;
import com.pur.zsms.supplier.po.ShopSupplierTypePo;
import com.pur.zsms.supplier.po.SupplierPo;
import com.pur.zsms.supplier.vo.SupplierVo;
import com.pur.zsms.systemsetting.datadictionary.po.Code;
import com.pur.zsms.vo.SupplierShopVo;

@Service("shopService")
public class ShopServiceImpl implements IShopService {

	String msg = "";

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Resource(name = "supplierDao")
	private SupplierDao supplierDao;
	@Resource(name = "shopDao")
	private IShopDao shopDaoImpl;

	@Resource(name = "userDao")
	private IUserDao userDaoImpl;

	@Resource(name = "materDao")
	private IMaterDao materDaoImpl;

	@Resource(name = "supplierShopDao")
	private SupplierShopDao supplierShopDao;

	@Resource(name = "userService")
	private IUserService userService;

	public IUserDao getUserDaoImpl() {
		return userDaoImpl;
	}

	public void setUserDaoImpl(IUserDao userDaoImpl) {
		this.userDaoImpl = userDaoImpl;
	}

	public IShopDao getShopDaoImpl() {
		return shopDaoImpl;
	}

	public void setShopDaoImpl(IShopDao shopDaoImpl) {
		this.shopDaoImpl = shopDaoImpl;
	}

	public PaginationSupport listShop(PageController pageController, ShopVo shopVo) throws GenericException {
		return shopDaoImpl.listShop(pageController, shopVo);
	}

	public PaginationSupport listShopForSup(PageController pageController, ShopVo shopVo) throws GenericException {
		return shopDaoImpl.listShopForSup(pageController, shopVo);
	}

	public PaginationSupport shopRaiseList(PageController pageController, ShopRaisePriceVo shopRaisePriceVo) throws GenericException {
		return shopDaoImpl.shopRaiseList(pageController, shopRaisePriceVo);

	}

	public boolean addShopForSup(ShopVo c, UserPo lguser) throws GenericException {

		// 同名检验
		List<ShopPo> listShopPo = shopDaoImpl.getShopPoByShopName(c.getShopName());
		if (listShopPo != null && listShopPo.size() > 0) {
			msg = "msg001";
			return false;
		}
		ShopPo shopPo = new ShopPo();
		shopPo.setProvince(c.getProvince());
		shopPo.setCity(c.getCity());
		shopPo.setCountry(c.getCountry());
		shopPo.setCompanyUser(c.getCompanyId());
		shopPo.setShopName(c.getShopName());
		shopPo.setCode(c.getCode());
		shopDaoImpl.addShopPo(shopPo);

		if (c.getLoginUser().equals("2")) {
			if ((c.getUserIdAll()) != null && !(c.getUserIdAll()).equals("")) {
				String[] userIdArray = c.getUserIdAll().split(",");
				for (String userId : userIdArray) {
					ShopUserPo shopUserPo = new ShopUserPo();
					shopUserPo.setShopId(shopPo.getShopId());
					shopUserPo.setUserId(Integer.valueOf(userId));
					// 先删除 这个用户其他的对应关系 然后更新
					shopDaoImpl.deleteShopUserPoByUserId(Long.valueOf(userId));
					shopDaoImpl.addShopUserPo(shopUserPo);
				}
			}
		} else if (c.getLoginUser().equals("3")) {
			UserVo tUserVo = new UserVo();
			tUserVo.setSupId((int) c.getSupId());
			tUserVo.setUserName(c.getUserName3());
			tUserVo.setPassword(c.getPassword());
			tUserVo.setUserPhone(Long.valueOf(c.getUserPhone()));
			tUserVo.setRealName(c.getShopName());
			tUserVo.setCity((int) c.getCity());
			tUserVo.setCountry((int) c.getCountry());
			tUserVo.setProvince((int) c.getProvince());
			tUserVo.setIsActive("0");
			tUserVo.setCompanyUser(c.getCompanyId());
			tUserVo.setLevelId(4);
			String[] roles = { "6" };
			tUserVo.setRoles(roles);
			long userId = userService.addUserForSup(tUserVo);
			ShopUserPo shopUserPo = new ShopUserPo();
			shopUserPo.setShopId(shopPo.getShopId());
			shopUserPo.setUserId(userId);
			// 先删除 这个用户其他的对应关系 然后更新
			shopDaoImpl.deleteShopUserPoByUserId(Long.valueOf(userId));
			shopDaoImpl.addShopUserPo(shopUserPo);

			SupplierShopUserPo supplierShopUserPo = new SupplierShopUserPo();
			supplierShopUserPo.setSupId(c.getSupId());
			supplierShopUserPo.setUserId(userId);
			supplierDao.addSupplierShopUserPo(supplierShopUserPo);
		}

		// 设置供货商与客户关系表
		SupplierShopPo supplierShopPo = new SupplierShopPo();
		supplierShopPo.setShopId(shopPo.getShopId());
		supplierShopPo.setCode(c.getCode());
		supplierShopPo.setSupId(c.getSupId());
		supplierShopPo.setSupName(c.getSupName());
		supplierShopDao.addSupplierShopDao(supplierShopPo);

		// 设置供货商种类与客户关系表
		// //先查询出供货商材料种类
		List<Object> materTypeList = materDaoImpl.getMaterTypeBySupId(c.getSupId());
		for (Object materType : materTypeList) {
			ShopSupplierTypePo shopSupplierTypePo = new ShopSupplierTypePo();
			shopSupplierTypePo.setShopId((int) shopPo.getShopId());
			shopSupplierTypePo.setSupId((int) c.getSupId());
			shopSupplierTypePo.setSupType(Integer.valueOf(materType.toString()));
			supplierDao.addShopSupplierTypePo(shopSupplierTypePo);
		}

		return true;
	}

	public boolean updateShopForSup(ShopVo c) throws GenericException {
		// 同名检验
		List<ShopPo> listShopPo = shopDaoImpl.getShopPoByShopName(c.getShopName());
		if (listShopPo != null && listShopPo.size() > 0 && listShopPo.get(0).getShopId() != c.getShopId()) {
			msg = "msg001";
			return false;
		}

		ShopPo shopPo = shopDaoImpl.getShopVoByShopId(c.getShopId());
		shopPo.setProvince(c.getProvince());
		shopPo.setCity(c.getCity());
		shopPo.setCountry(c.getCountry());
		shopPo.setShopName(c.getShopName());
		shopPo.setCode(c.getCode());
		shopDaoImpl.updateShopPo(shopPo);
		// 先删除关系表中的数据
		shopDaoImpl.deleteShopUserPoByShopId(c.getShopId());

		if ((c.getUserIdAll()) != null && !(c.getUserIdAll()).equals("")) {
			String[] userIdArray = c.getUserIdAll().split(",");
			for (String userId : userIdArray) {
				ShopUserPo shopUserPo = new ShopUserPo();
				shopUserPo.setShopId(shopPo.getShopId());
				shopUserPo.setUserId(Integer.valueOf(userId));
				// 先删除 这个用户其他的对应关系 然后更新
				shopDaoImpl.deleteShopUserPoByUserId(Long.valueOf(userId));

				shopDaoImpl.addShopUserPo(shopUserPo);
			}
		}

		// 设置供货商与客户关系
		SupplierShopVo vo = new SupplierShopVo();
		vo.setShopId(shopPo.getShopId());
		vo.setSupId(c.getSupId());
		SupplierShopPo supplierShopPo = supplierShopDao.getSupplierShopPo(vo).get(0);
		supplierShopPo.setShopId(shopPo.getShopId());
		supplierShopPo.setCode(c.getCode());
		supplierShopPo.setSupId(c.getSupId());
		supplierShopPo.setSupName(c.getSupName());
		supplierShopDao.updateSupplierShopDao(supplierShopPo);
		return true;
	}

	public boolean addShop(ShopVo c, UserPo lguser) throws GenericException {

		// 同名检验
		List<ShopPo> listShopPo = shopDaoImpl.getShopPoByShopName(c.getShopName());
		if (listShopPo != null && listShopPo.size() > 0) {
			msg = "msg001";
			return false;
		}

		long companyUser = userDaoImpl.getCompanyUser(lguser).get(0).getCompanyUser();
		ShopPo shopPo = new ShopPo();
		shopPo.setProvince(c.getProvince());
		shopPo.setCity(c.getCity());
		shopPo.setCountry(c.getCountry());
		shopPo.setCompanyUser(companyUser);
		shopPo.setShopName(c.getShopName());
		shopPo.setCode(c.getCode());
		shopDaoImpl.addShopPo(shopPo);
		if ((c.getUserIdAll()) != null && !(c.getUserIdAll()).equals("")) {
			String[] userIdArray = c.getUserIdAll().split(",");
			for (String userId : userIdArray) {
				ShopUserPo shopUserPo = new ShopUserPo();
				shopUserPo.setShopId(shopPo.getShopId());
				shopUserPo.setUserId(Integer.valueOf(userId));
				// 先删除 这个用户其他的对应关系 然后更新
				shopDaoImpl.deleteShopUserPoByUserId(Long.valueOf(userId));
				shopDaoImpl.addShopUserPo(shopUserPo);
			}
		}

		return true;
	}

	public boolean updateShop(ShopVo c) throws GenericException {
		// 同名检验
		List<ShopPo> listShopPo = shopDaoImpl.getShopPoByShopName(c.getShopName());
		if (listShopPo != null && listShopPo.size() > 0 && listShopPo.get(0).getShopId() != c.getShopId()) {
			msg = "msg001";
			return false;
		}

		ShopPo shopPo = shopDaoImpl.getShopVoByShopId(c.getShopId());
		shopPo.setProvince(c.getProvince());
		shopPo.setCity(c.getCity());
		shopPo.setCountry(c.getCountry());
		shopPo.setShopName(c.getShopName());
		shopPo.setCode(c.getCode());
		shopDaoImpl.updateShopPo(shopPo);
		// 先删除关系表中的数据
		shopDaoImpl.deleteShopUserPoByShopId(c.getShopId());

		if ((c.getUserIdAll()) != null && !(c.getUserIdAll()).equals("")) {
			String[] userIdArray = c.getUserIdAll().split(",");
			for (String userId : userIdArray) {
				ShopUserPo shopUserPo = new ShopUserPo();
				shopUserPo.setShopId(shopPo.getShopId());
				shopUserPo.setUserId(Integer.valueOf(userId));
				// 先删除 这个用户其他的对应关系 然后更新
				shopDaoImpl.deleteShopUserPoByUserId(Long.valueOf(userId));

				shopDaoImpl.addShopUserPo(shopUserPo);
			}
		}
		return true;
	}

	public ShopVo getShopVoByShopId(long shopId) throws GenericException {
		ShopVo shopVo = new ShopVo();
		ShopPo shopPo = shopDaoImpl.getShopVoByShopId(shopId);
		BeanCopier copier = BeanCopier.create(ShopPo.class, ShopVo.class, false);
		copier.copy(shopPo, shopVo, null);

		List<UserPo> userList = userDaoImpl.getUserForShopByShopId(shopId);
		StringBuffer userName = new StringBuffer("");
		StringBuffer userIdAll = new StringBuffer("");
		if (userList != null && userList.size() > 0) {

			for (UserPo userPo : userList) {
				userName.append(userPo.getRealName());
				userName.append(",");
				userIdAll.append(userPo.getUserId());
				userIdAll.append(",");
			}
			shopVo.setUserName(userName.substring(0, userName.length() - 1));
			shopVo.setUserIdAll(userIdAll.substring(0, userIdAll.length() - 1));
		} else {
			shopVo.setUserName("");
			shopVo.setUserIdAll("");
		}

		return shopVo;
	}

	public ShopRaisePriceVo getShopRaisePriceVoByRpid(long rpid) throws GenericException {
		ShopRaisePricePo po = shopDaoImpl.getShopRaisePricePoByRpid(rpid).get(0);
		ShopRaisePriceVo vo = new ShopRaisePriceVo();
		BeanCopier copier = BeanCopier.create(ShopRaisePricePo.class, ShopRaisePriceVo.class, false);
		copier.copy(po, vo, null);
		return vo;
	}

	public long getShopIdoByUserId(long userId) throws GenericException {
		ShopUserPo shopuserPo = shopDaoImpl.getShopUserPoByUserId(userId).get(0);
		return shopuserPo.getShopId();
	}

	public List<ShopUserPo> getShopUserPoByUserId(long userId) throws GenericException {
		return shopDaoImpl.getShopUserPoByUserId(userId);
	}

	public ShopPo getShopPo(long shopId) throws GenericException {
		return shopDaoImpl.getShopPoByShopId(shopId).get(0);
	}

	public ShopPo getShopPoByOrdId(long ordId) throws GenericException {
		return shopDaoImpl.getShopPoByOrdId(ordId).get(0);
	}

	public boolean addShopRaise(ShopRaisePriceVo c, UserPo lguser) throws GenericException {

		// 检验
		List<ShopRaisePricePo> listShopRaisePricePo = shopDaoImpl.getShopRaisePricePoByShopIdMaterTypeSupId(c.getShopId(), c.getMaterType(),
				c.getSupId());
		if (listShopRaisePricePo != null && listShopRaisePricePo.size() > 0) {
			msg = "msg001";
			return false;
		}
		ShopRaisePricePo po = new ShopRaisePricePo();
		BeanCopier copier = BeanCopier.create(ShopRaisePriceVo.class, ShopRaisePricePo.class, false);
		RoleVo dto = new RoleVo();
		copier.copy(c, po, null);

		shopDaoImpl.addShopRaisePricePo(po);
		return true;
	}

	public boolean updateShopRaise(ShopRaisePriceVo c, UserPo lguser) throws GenericException {
		ShopRaisePricePo po = shopDaoImpl.getShopRaisePricePoByRpid(c.getRpid()).get(0);
		po.setRaiseAmount(c.getRaiseAmount());
		shopDaoImpl.updateShopRaisePricePo(po);
		return true;
	}

	public void deleteShopRaise(long rpid) throws GenericException {
		shopDaoImpl.deleteShopRaise(rpid);
	}

	public String getShopIdBycompanyId(String companyId) throws GenericException {
		List<ShopPo> shopList = shopDaoImpl.getShopPoBycompanyId(companyId);
		StringBuffer jsonUserTestValue = new StringBuffer("");

		if (shopList != null && shopList.size() > 0) {

			jsonUserTestValue.append("[");

			for (ShopPo shopPo : shopList) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(shopPo.getShopId());
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append(shopPo.getShopName());
				jsonUserTestValue.append("\"},");
			}
			jsonUserTestValue = new StringBuffer(jsonUserTestValue.substring(0, jsonUserTestValue.length() - 1));
			jsonUserTestValue.append("]");
		}
		return jsonUserTestValue.toString();
	}

	public String getShopIdBycompanyIdSupid(String companyId, long supId) throws GenericException {
		List<ShopPo> shopList = shopDaoImpl.getShopPoBycompanyIdSupId(companyId, supId);
		StringBuffer jsonUserTestValue = new StringBuffer("");

		if (shopList != null && shopList.size() > 0) {

			jsonUserTestValue.append("[");

			for (ShopPo shopPo : shopList) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(shopPo.getShopId());
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append(shopPo.getShopName());
				jsonUserTestValue.append("\"},");
			}
			jsonUserTestValue = new StringBuffer(jsonUserTestValue.substring(0, jsonUserTestValue.length() - 1));
			jsonUserTestValue.append("]");
		}
		return jsonUserTestValue.toString();
	}

	public String getShopIdBycompanyIdAllSupId(String companyIdAll, long supId) throws GenericException {
		List<ShopPo> shopList = shopDaoImpl.getShopIdBycompanyIdAllSupId(companyIdAll, supId);
		StringBuffer jsonUserTestValue = new StringBuffer("");

		if (shopList != null && shopList.size() > 0) {

			jsonUserTestValue.append("[");

			for (ShopPo shopPo : shopList) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(shopPo.getShopId());
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append(shopPo.getShopName());
				jsonUserTestValue.append("\"},");
			}
			jsonUserTestValue = new StringBuffer(jsonUserTestValue.substring(0, jsonUserTestValue.length() - 1));
			jsonUserTestValue.append("]");
		}
		return jsonUserTestValue.toString();
	}

	public boolean getCheckPhone(String shopName) throws GenericException {
		List<ShopPo> list = shopDaoImpl.getShopPoByShopName(shopName);
		if (list != null && list.size() > 0) {
			return false;
		}
		return true;
	}

}
